package com.df.bpm.utils.businessViewerV2.entity;

import com.df.bpm.utils.businessViewerV2.Main;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.List;

public class Db_entity {

    public M_entity get(Integer businessEntityIndex) {
        RestTemplate restTemplate = new RestTemplate();
        String url= Main.server+"businessEntity/id?businessEntityIndex="+businessEntityIndex;
        ResponseEntity<M_entity> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<M_entity>() {}
        );
        return response.getBody();
    }

    public List<M_entityDto> getList4Like(M_entityDto temp) {
        RestTemplate restTemplate = new RestTemplate();
        String tempString=Main.printNonNullFields(temp);
        String url=Main.server+"businessEntity/dto4Like?"+tempString;
        ResponseEntity<List<M_entityDto>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<List<M_entityDto>>() {}
        );
        return response.getBody();
    }

    public Integer update(M_entity temp) {
        if(temp.getName()!=null
                && !temp.getName().isEmpty()
        ){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+"businessEntity/";
            HttpEntity<M_entity> request = new HttpEntity<>(temp, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.PUT,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    public Integer delete(Integer businessEntityIndex) {
        if(businessEntityIndex!=null ){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+"businessEntity?businessEntityIndex="+businessEntityIndex;
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.DELETE,
                    null, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }
}
